package com.baosight.sczx.zm.cc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cc.OperationHistory;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblem;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandle;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandleProcess_x;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemReview;

/**
 * 
 * <p>Title: ServiceZMCC30.java</p>
 * <p>Description: 生产问题核查</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月28日 下午1:43:59  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCC30 extends ServiceEPBase {
	private static Dao dao = (Dao) SpringApplicationContext.getBean("dao");

	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCC30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:44:24 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的

		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询 方法 </p>
	 * <p>return value: ServiceZMCC30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:44:34 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {

		EiInfo outInfo = new EiInfo();
		EiBlock resultBlock = new EiBlock("result");
		ZMCCProduceproblemHandleProcess_x _dom = new ZMCCProduceproblemHandleProcess_x();
		resultBlock.addBlockMeta(_dom.eiMetadata);
		Map<String, String> _args = new HashMap<String, String>();
		_args.put("fdProblemStatus", "4");
		_args.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());

		List<ZMCCProduceproblem> _result = dao.query("ZMCCProduceproblem.query", _args);
		for (int i = 0; i < _result.size(); i++) {
			ZMCCProduceproblem record = (ZMCCProduceproblem) _result.get(i);
			Map<String, String> process_args = new HashMap<String, String>();
			process_args.put("fdRecordId", record.getFdKeyId());
			List<ZMCCProduceproblemHandleProcess_x> process_result = dao.query("ZMCCProduceproblemHandleProcess_x.query", process_args);
			resultBlock.addRows(process_result);
		}
		outInfo.addBlock(resultBlock);

		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType", false));// 问题类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel", false));// 问题级别
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.problemStatus", new String[]{"0", "1", "2", "3", "4", "5", "6"}, new String[]{"草拟", "未处理", "未查收", "未整改", "未核查", "关闭", "作废"}, true));//问题状态
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.isCheck", new String[] { "否", "是" }, new String[] { "否", "是" }, true));// 是否重复问题
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemOrigin"));// 问题来源
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));

		return outInfo;
	}

	/**
	 * 
	 * <p>Function: recheck</p>
	 * <p>Description: 生产问题处理 填写核查意见</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:45:10 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo recheck(EiInfo inInfo) {

		EiInfo out = new EiInfo();
		ZMCCProduceproblemReview _dom = new ZMCCProduceproblemReview();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);

		_dom.fromMap(eiBlock.getRow(0));

		String fdRiskRecordId = _dom.getFdRecordId();// 生产问题记录ID
		String fdIsRedo = _dom.getFdIsRedo();// 是否重新整改
		String fdIsRepeat = eiBlock.getCellStr(0, "fdIsRepeat");// 是否重复隐患,该字段不在当前表中
		String fdIsPenalty = eiBlock.getCellStr(0, "fdIsPenalty");// 是否罚款

		boolean value = this.canRecheck(fdRiskRecordId);// 是否====没有被关闭

		Map<String, String> params1 = new HashMap<String, String>();
		params1.put("fdIsRepeat", fdIsRepeat);
		params1.put("fdKeyId", fdRiskRecordId);
		if (value) {
			params1.put("fdProblemStatus", "5");
			int result = this.dao.update("ZMCCProduceproblem.modifyRecheck", params1);
			if (result != -1) {
				// 复查表插入
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_REVIEW"));
				this.dao.insert("ZMCCProduceproblemReview.insert", _dom);
				// 操作记录
				boolean insertValue = OperationHistory.InsertOperationHistory(_dom.getFdRecordId(), "生产问题", "复查");// 新增隐患处理记录
				if (!insertValue) {
					inInfo.setStatus(-1);
					inInfo.setMsg("插入操作记录失败！");
					return inInfo;
				}
				// 更改处理表
				Map<String, String> _argMap = new HashMap<String, String>();
				_argMap.put("fdKeyId", _dom.getFdRecordHandleId());
				this.dao.update("ZMCCProduceproblemHandle.modifyRechecked", _argMap);

				// 更新罚款记录
				if (fdIsPenalty.equals("是")) {
					Map<String, String> _argMap1 = new HashMap<String, String>();
					_argMap1.put("fdKeyId", _dom.getFdRecordHandleId());
					List<ZMCCProduceproblemHandle> handleResult = this.dao.query("ZMCCProduceproblemHandle.query", _argMap1);
					if (handleResult.size() > 0) {
						String fdIsSubmit = eiBlock.getCellStr(0, "fdIsSubmit");// 是否已交罚款
						Map<String, String> params2 = new HashMap<String, String>();
						params2.put("fdIsSubmit", fdIsSubmit);
						params2.put("fdKeyId", handleResult.get(0).getFdPenaltyId());
						this.dao.update("ZMCCProduceproblemPenalty.updateIsSubmit", params2);
					}
				}
			}
		}

		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: canRecheck</p>
	 * <p>Description: 是否能复查，避免同时操作时 该记录已经被复查</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:45:42 郭淑芳
	 * </p>
	 *
	 */
	private boolean canRecheck(String fdKeyId) {
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdRecordId", fdKeyId);

		List<?> _result = dao.query("ZMCCProduceproblemHandle.canRecheck", _argMap);

		if (_result.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * <p>Function: queryForDetail</p>
	 * <p>Description: 查询主表明细数据</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:45:58 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo queryForDetail(EiInfo inInfo) {

		// 查询当前记录的数据，进行展示
		String fdRecordId = inInfo.getString("fdRecordId");
		String fdRecordHandleId = inInfo.getString("fdRecordHandleId");
		String fdRecordModifyId = inInfo.getString("fdRecordModifyId");
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdRecordId", fdRecordId);
		_argMap.put("fdRecordHandleId", fdRecordHandleId);
		_argMap.put("fdRecordModifyId", fdRecordModifyId);

		List<ZMCCProduceproblemHandleProcess_x> _result = dao.query("ZMCCProduceproblemHandleProcess_x.query", _argMap);
		EiBlock eiBlock = new EiBlock("result");
		ZMCCProduceproblemHandleProcess_x _dom = new ZMCCProduceproblemHandleProcess_x();
		eiBlock.setBlockMeta(_dom.eiMetadata);
		ZMCCProduceproblemHandleProcess_x args = _result.get(0);

		args.setFdRecheckerDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 复查部门ID
		args.setFdRecheckerDepartment(UserSession.getInSessionProperty("orgName").toString());// 复查部门
		args.setFdRecheckPeople(UserSession.getInSessionProperty("UserCname").toString());// 复查人
		args.setFdRecheckPeopleId(UserSession.getUserId().toString());// 复查人ID
		args.setFdRecheckDate(StaticTools.getDate()); // 复查日期
		eiBlock.addRow(args);

		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(eiBlock);
		return outInfo;
	}
}
